//
//  B_IntegralInComeVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/27.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_IntegralInComeVC.h"
#import "B_IntegralInComeTVC.h"
#import "B_IntegralInComeModel.h"

@interface B_IntegralInComeVC ()
@property (nonatomic, strong) BaseTableView * mainView;

@end

@implementation B_IntegralInComeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = self.mainView;
    self.dataArray = [NSMutableArray array];
    [self getData:YES];
}


- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:0];
        
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_IntegralInComeTVC" bundle:nil] forCellReuseIdentifier:@"B_IntegralInComeTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 1;
        };
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;;
        };
        
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_IntegralInComeModel * model = weSelf.dataArray[indexPath.row];
            
            B_IntegralInComeTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_IntegralInComeTVC" forIndexPath:indexPath];
            cell.B_titleLabel.text = model.content;
            cell.B_timeLabel.text = [NSObject getTime:model.createTime];
            cell.B_integralLabel.text = [NSString stringWithFormat:@"+%@", safeValue1(model.integral)];
            return cell;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 75;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            
        };
        
        [_mainView setTableViewHeaderFresh:^{
            [weSelf getData:YES];
        }];
        [_mainView setTableViewFooterFresh:^{
            [weSelf getData:NO];
        }];
        
    }
    return _mainView;
}

- (void) getData:(BOOL) isrefresh {
    if (isrefresh) {
        self.index = 1;
    } else {
        self.index ++;
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userIntegral/userIntegralDetailIncome/%@/%ld/10", getUserId(), self.index] parameters:nil isCache:isrefresh succeed:^(id data) {
        if (isrefresh) {
            [weSelf.dataArray removeAllObjects];
        }
        [weSelf.mainView endRefresh];
        if ([data[@"code"] isEqualToString:@"20010"]) {
            for (NSDictionary * dictionary in data[@"values"]) {
                B_IntegralInComeModel * model = [B_IntegralInComeModel mj_objectWithKeyValues:dictionary];
                [weSelf.dataArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.mainView endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
